﻿using System;
using System.Text;
using System.Collections.Generic;
using Checkers.Game;
using Checkers.Common;

namespace Checkers.View.ConsoleView
{
    public class ConsoleFlow
    {
        // Members
        private CheckersGame m_Game;

        public ConsoleFlow()
        {
        }

        public void Start()
        {
            bool userExited = false;

            // Welcome the user
            ConsoleDisplay.ShowWelcome();

            // Init the game
            int boardSize = ConsoleDisplay.GetBoardSize();
            if (boardSize == ConsoleDisplay.sk_NumericExitCode)
            {
                // If the user exited
                userExited = true;
            }
            else
            {
                // Set the game type
                eGameType gameType = ConsoleDisplay.GetGameType();

                // Init the game
                m_Game = new CheckersGame(boardSize, gameType);

                // Start a new game until the user wishes to quit
                while (!userExited)
                {
                    // Play a game
                    m_Game.StartSingleGame();
                    playSingleGame();

                    // Show mid summary
                    ConsoleDisplay.ShowMidSummary(m_Game);

                    // Ask to resume
                    userExited = ConsoleDisplay.GetPlayAgain();
                }

                // Show games summary
                ConsoleDisplay.ShowTotalSummary(m_Game);
            }

            // End of all games
            ConsoleDisplay.ShowGoodbye();
        }

        private void playSingleGame()
        {
            // Show start state
            ConsoleDisplay.ShowNewGame(m_Game);
            GameMove currentMove = null;

            // Get moves and play untill game is over or the user forfeits
            do
            {
                // TODO : Maybe get rid of location class
                // TODO : Why list ?
                currentMove = ConsoleDisplay.GetMove(m_Game.CurrentPlayer);
                List<GameMove> moves = new List<GameMove>();
                moves.Add(currentMove);

                if (currentMove == null)
                {
                    // The user forfeits
                    m_Game.ForfeitGame();
                }
                else if (!isMoveInsideBoard(currentMove))
                {
                    // The move is not within the board's bounds
                    ConsoleDisplay.ShowMoveRangeError();
                }
                else if (!m_Game.RunASingleTurn(moves))
                {
                    // The move is illegal
                    ConsoleDisplay.ShowMoveIllegalError();
                }
                else
                {
                    // The move was made, switch turns and show the new board
                    ConsoleDisplay.ClearScreen();
                    ConsoleDisplay.PrintBoard(m_Game.GameBoard);
                }
            } 
            while (m_Game.GameStatus == eGameStatuses.Active);
        }

        private bool isMoveInsideBoard(GameMove currentMove)
        {
            // TODO : Implement
            return true;
        }
    }
}
